package com.browsesoft.doaction;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;

import com.browsesoft.htmlcomponent.HTMLBasicComponent;

public class DoActionHelper {

	/**
	 * 根据参数和配置产生动作类
	 */
	public static DoAction createDoAction(Element doActionElem, String doAction) {
		try {
			String className = doActionElem.getAttribute("classname");
			DoAction da = (DoAction) Class.forName(className).newInstance();
			return da;
		} catch (Exception e) {
			throw new RuntimeException("创建动作类" + doAction + "发生错误", e);
		}
	}

	/**
	 * @param component
	 *            组建元素
	 * @param doAction
	 *            动作参数
	 * @param request
	 * @param objParam
	 *            动作类所需的对象参数
	 */

	public static void doAction(HTMLBasicComponent component, String doAction,
			HttpServletRequest request, Object objParam) throws Exception {
		Element doActionElement = (Element) component.getPage().getElement()
				.getElementsByTagName(doAction).item(0);
		DoAction da = createDoAction(doActionElement, doAction);
		String message = da.doAction(doActionElement, request, objParam);
		// 如果有动作处理后的提示信息
		if (message != null && !message.equals("")) {
			component.getPage().showMessage("alert('" + message + "')");
		}
	}
}
